package com.my.mvpframe.ui.main;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.my.mvpframe.R;
import com.my.mvpframe.base.app.BaseFragment;
import com.my.mvpframe.base.dagger.ActivityScoped;
import com.my.mvpframe.db.AppDatabase;
import com.my.mvpframe.db.User;
import com.my.mvpframe.db.UserDao;
import com.my.mvpframe.mvp.base.BasePresenter;
import com.my.mvpframe.rxbus.RxBus;
import com.my.mvpframe.rxbus.RxBusConstants;
import com.my.mvpframe.ui.lesson.LessonActivity;
import com.my.mvpframe.utils.ToastUtil;

import java.util.Date;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Created by tomey on 2018/2/1.
 */

@ActivityScoped
public class MainFragment extends BaseFragment implements MainContract.View {

    @BindView(R.id.textview)
    TextView textview;
    @BindView(R.id.textview1)
    TextView textview1;
    @BindView(R.id.textview4)
    TextView textview4;

    private UserDao userDao;

    @Inject
    MainContract.Presenter presenter;

    @Inject
    public MainFragment(){}


    @Override
    public void showErrorTip(String msg) {

    }

    @Override
    public void showTvResult() {

    }

    @Override
    public void showToast(String str) {
        ToastUtil.ShowLong(getContext(),str);
    }

    @Override
    protected void initView() {
        userDao = AppDatabase.getInstance(getContext()).userDao();

        RxBus.getIntanceBus().register(RxBusConstants.TYPE_ONE, (Consumer<String>) o -> textview.setText(o));
        RxBus.getIntanceBus().register(RxBusConstants.TYPE_TWO, (Consumer<String>) o -> textview.setText(o));
        RxBus.getIntanceBus().register(RxBusConstants.TYPE_THREE, (Consumer<String>) o -> textview.setText(o));

        Date date = new Date();
        long time = date.getTime() / 1000;

        presenter.getJokeData("1009eb9e83f3f2ce58a011f87b1f44fa",1,10,"asc",time+"");
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.takeView(this);
    }

    @Override
    protected BasePresenter initPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @OnClick({R.id.textview1, R.id.textview2, R.id.textview3, R.id.textview4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textview1:// 添加第一组数据
                final User user = new User(1, "Z", "J", "18", null);
                presenter.getInsertUser(userDao, user);
                RxBus.getIntanceBus().post(RxBusConstants.TYPE_ONE, "我是" + user.getFirstName().toString());
                break;
            case R.id.textview2:// 添加第二组数据
                User user1 = new User(2, "T", "Y", "18", null);
                presenter.getInsertUser(userDao, user1);
                RxBus.getIntanceBus().post(RxBusConstants.TYPE_TWO, "我是" + user1.getFirstName().toString());
                break;
            case R.id.textview3:
                presenter.getAllUser(userDao);
                RxBus.getIntanceBus().post(RxBusConstants.TYPE_THREE, "我是-------");
                break;
            case R.id.textview4:
                RxBus.getIntanceBus().unRegister(RxBusConstants.TYPE_ONE);
                startActivity(new Intent(getContext(), LessonActivity.class));
                break;
        }
    }
}
